package cn.stronglink.assetmanage.business.check.view.checked;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.stronglink.assetmanage.MainActivity;
import cn.stronglink.assetmanage.R;
import cn.stronglink.assetmanage.business.check.adapter.CheckListAdapter;
import cn.stronglink.assetmanage.business.check.adapter.CheckOrderAdapter;
import cn.stronglink.assetmanage.business.check.model.CheckedModel;
import cn.stronglink.assetmanage.business.check.presenter.CheckedListPresenter;
import cn.stronglink.assetmanage.business.check.view.sync.SyncAssetFragment;
import cn.stronglink.assetmanage.entity.Check;
import cn.stronglink.assetmanage.index.base.BaseChildFragment;
import cn.stronglink.assetmanage.index.base.BaseFragment;
import cn.stronglink.assetmanage.index.base.BasePresenter;
import cn.stronglink.assetmanage.index.entity.PromissionEntity;
import cn.stronglink.assetmanage.index.presenter.MenuPresenter;
import cn.stronglink.assetmanage.index.view.onRecyclerListener;
import cn.stronglink.assetmanage.utils.Constants;
import cn.stronglink.assetmanage.utils.ToastUtil;

/**
 * 入库表单列表
 */
public class CheckedListFragment extends BaseChildFragment {
    private CheckListAdapter listAdapter;
    private CheckedModel checkedModel;
    private CheckedListView checkedListView;
    private CheckedListPresenter checkedListPresenter;
    //入库单列表
    private RecyclerView rv_checked_in_order_list;
    private LinearLayoutManager layoutManager;
    private TextView txt_title_right, txt_title_left, txt_main_title;
    private List<Check> checkList;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_checked_list, container, false);
        initViewAndData(rootView);
        return rootView;
    }

    private void initViewAndData(View view) {
        try {
            checkList = new ArrayList<>();
            txt_title_right = this.getActivity().findViewById(R.id.txt_right_title);
            txt_title_right.setVisibility(View.INVISIBLE);
            txt_title_left = this.getActivity().findViewById(R.id.txt_left_title);
            txt_title_left.setVisibility(View.INVISIBLE);
            txt_main_title = this.getActivity().findViewById(R.id.txt_main_title);
            txt_main_title.setText("验收入库");
            listAdapter = new CheckListAdapter(this.getActivity());
            rv_checked_in_order_list = view.findViewById(R.id.rv_checked_in_order_list);
            layoutManager = new LinearLayoutManager(this.getActivity(), LinearLayout.VERTICAL, false);
            rv_checked_in_order_list.setLayoutManager(layoutManager);
            rv_checked_in_order_list.setAdapter(listAdapter);
            listAdapter.setOnItemClickListener(new onRecyclerListener() {
                @Override
                public void onItemClicked(View v, int postion) {
                    if (checkList != null && checkList.size() > 0) {
                        MainActivity activity = (MainActivity) getActivity();
                        PromissionEntity pe = new PromissionEntity();
                        pe.setFragment(new CheckedAssetFragment());
                        pe.setPosition(MainActivity.FRAGMENT_CUSTOM_ID);
                        pe.setTitle("已入库");
                        Bundle bundle = new Bundle();
                        bundle.putSerializable(Constants.CHECK_IN_DETAIL_NO, checkList.get(postion).getId_());
                        pe.setBundle(bundle);
                        activity.onEventRegister(pe);
                    }
                }
            });
            checkedModel = new CheckedModel();
            checkedListView = new CheckedListView(this, view, listAdapter);
            checkedListPresenter = new CheckedListPresenter(checkedListView, checkedModel);

        } catch (
                Exception e)

        {
            e.printStackTrace();
            ToastUtil.toastShort("没有数据显示");
        }

    }

    @Override
    protected void onFragmentVisibleChange(boolean isVisible) {
        if (isVisible == false) return;
        try {
            if (checkedModel != null && checkedListView != null) {
                checkList = checkedModel.getDataByOrderNoDistince();
                checkedListView.setListData(checkList);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }
}
